package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.annotation.Permission;
import com.erp.pojo.RoleMenus;
import com.erp.pojo.Roler;
import com.erp.service.RoleMenusService;
import com.erp.service.RolerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@CrossOrigin
public class RolerController {
    @Autowired
    private RolerService rolerService;
    @Autowired
    private RoleMenusService roleMenusService;

    /*处理分页查询请求*/
    @GetMapping("/rolerList")
    public Map<String,Object> rolerList(
        @RequestParam(defaultValue = "1") Integer pageNum
        , @RequestParam(defaultValue = "3") Integer pageSize){
        return rolerService.queryRolePageListService(pageNum,pageSize);
    }

    @PostMapping("/updateRoler")
    public Map<String,Object> updateRoler(@RequestBody Roler roler){

        Map<String,Object> result=new HashMap<>();
        result.put("code",400);
        result.put("msg","操作失败.......");
        try{
            rolerService.updateById(roler);
            result.put("code",200);
            result.put("msg","更新角色信息成功......");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;

    }

    @Permission(role = "admin")
    /*处理角色信息添加的请求*/
    @PostMapping("/saveRoler")
    public Map<String,Object> saveRoler(@RequestBody Roler roler){
        Map<String,Object> result=new HashMap<>();
        result.put("code",400);
        result.put("msg","操作失败.......");
        try{
            rolerService.save(roler);
            result.put("code",200);
            result.put("msg","保存角色信息成功......");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
    /*处理角色信息删除的请求*/
    @Autowired
    private RoleMenusService roleMenuService;
    /*处理角色信息删除的请求*/
    @GetMapping("/deleteRoler/{id}")
    public Map<String,Object> deleteRoler(@PathVariable Integer id){
        Map<String,Object> result=new HashMap<>();
        result.put("code",400);
        result.put("msg","操作失败.......");
        try{
            //删除角色信息
            rolerService.removeById(id);
            //删除角色对应的菜单信息
            QueryWrapper<RoleMenus> cond=new QueryWrapper<>();
            cond.eq("rid",id);
            roleMenuService.remove(cond);

            result.put("code",200);
            result.put("msg","删除角色信息成功......");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
    /*处理角色授权请求*/
    /*处理角色授权请求*/
    @PostMapping("/grantRoleMenus")
    public Map<String,Object> grantRoleMenus(@RequestBody Integer[] ids){
        System.out.println("ids="+ Arrays.toString(ids));
        Map<String,Object> result=new HashMap<>();
        result.put("code",400);
        result.put("msg","操作失败.......");
        try{
            //数组中获得第一个元素，角色id
            Integer  rid=ids[0];

            List<RoleMenus> list=new ArrayList<>();

            //for循环便利
            for(int x=1;x<ids.length;x++){
                //完成角色授权，给角色和菜单的中间关系添加数据，建立关系
                RoleMenus rm=new RoleMenus();
                rm.setRid(rid);
                rm.setMid(ids[x]);
                //将rm对象添加到list集合
                list.add(rm);

            }

            //创建对象，封装删除的where条件
            QueryWrapper<RoleMenus> cond=new QueryWrapper<>();
            cond.eq("rid",rid);
            //断开该角色和菜单的关系，执行删除
            roleMenusService.remove(cond);

            //建立角色和菜单的关系
            roleMenusService.saveBatch(list); //批量添加

            result.put("code",200);
            result.put("msg","角色授权成功......");
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }

    /*处理根据角色id查询所有菜单id集合*/
    @GetMapping("/loadRoleMid/{rid}")
    public List<Integer> loadRoleMid(@PathVariable Integer rid){

      /*  QueryWrapper<Roler> wrapper=new QueryWrapper<>();
        wrapper.eq("rid",rid);
        wrapper.select("mid");
        List<Integer> result = rolerService.listObjs(wrapper);*/

//        QueryWrapper<RoleMenus> wrapper=new QueryWrapper<>();
//        wrapper.eq("rid",rid);
//        wrapper.select("mid");
//        List<Integer> result =roleMenuService.listObjs(wrapper);
        return roleMenusService.queryRoleMidsListService(rid);
    }


    /*加载所有角色信息*/
    @GetMapping("/loadAllRoles")
    public List<Roler> loadAllRoles(){
        QueryWrapper<Roler> wrapper=new QueryWrapper<>();
        wrapper.select("id","rname");
        List<Roler> list = rolerService.list(wrapper);
        return list;
    }


}
